package com.hhkj.payment.alipay.common.conf;

import com.ruoyi.common.exception.GlobalException;
import lombok.Getter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * AliPayConf
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/11/15 15:51
 */
@Component
@ConfigurationProperties(prefix = "payment.alipay")
public class AliPayConf {

    /** 开放平台上创建的应用的ID */
    @Getter
    private static String appId = "2021004125622328";

    /** 私钥路径 */
    private static String privateKeyPath = "D:\\Company\\aliPay\\证书20231116103601\\应用私钥RSA2048.txt";

    /** 应用私钥 */
    private static String privateKey; // = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCX6SJ+fKIzwdjyB9fb0Fjc8Q7YN8+nQSeBNJMU1jkb3wjkk6HOi5KyzhNxN+UU5quw1YeoK6/+FGj8KPgUUzV8Ci7Rc7vl9eeHliciqe5g0q6aQNg4WFlWXnEOGJ/AbyTFaLnlEXswB6M7VOOaIgSlnhNu9xjyKv1C/IHnvdek/52J7/xiHVJQCnIOCaJO18m59CP1SzgBsXL2MafZ7T35STluAaqrWugUxwr+OyhbeHw58Egpv7vcsMiNsEZdqP5vVYhXgQjUsfgZ0jD46Wtqfx/LAUk0rdZE85GOJEXXvCSJa++eG0eYee9qTCBejZndfhafvvaXSfaetP4EvC0nAgMBAAECggEAGWNsUWlxDmdiY6jt6Z3bjCl5iqdhPBnGiXCI+F2JM+BFszX9P/DRtrJ8S1rFHYYjwa0eO/v5MOSmWyZZt8PkV3O4OWc3SwqbBG0S41c1MNdlOQCHYntZivbDvFBfOqEYqymCkAco6HEOVdctJtByqF9P7ia785U9uJlM+LcguEGAyd8vd5GMjfa2lDY9JXGRG0kZnml4pDStPcLKkXk/WQu5s7RNPJQH8m4raC700qcj2UzjXH7jRMzvO/XCUyAUiI/qZLljr7jHwNA0gN8AkGX4CBF5Q9wyiSEOSZ+rzr4k1tYirtygokD4bHv6OKBPGsIlfcBuec+mo5TDh0yFOQKBgQD1adPtvGNc4kl7Y9/YlYhfKiDFhv1ZC6F4nasEYq+PhOdksCLKS4VSJ1f2lal0/K3rdYOdz/b0S3VorA3IM/nH10vDf+C8M1HIVlMhletRSmfMRHcn99Dvnla8oCuRWdmdJWlWN5+XYEwnyufqLiSSSYAy8fV6qUcBe8zxQrOBXQKBgQCedrqh4IgOKxbAay2o0psnanKzupcYlQkMlu4T865Rcr6OVVkpylw/Cm/C5mYQWwd2dnunuVlYNEY98mSecW3HKWe6T0aJ+xeahApsaGc4x+TU2GDqib93M+LYpz87wzuEohaeDTFOIKDlt0TURGeiL6u0m9yw6OPWj0CbJG5sUwKBgQCCArl6RYYiK+Mf0i5BEB65w5iu7C+FH+s/+c8iuKEnGBJ5vfIAV3feqOcpw+mNB1uf1JaQYR8EFoGxnQyEFjzqSL1jaXiGW22lA05xCXori45noZG6g8eVWb27meLa0s29OZePTFJooGRX5F9uKVnIgdW/p30TqV/rgkl1Oo/cJQKBgAxBCjWyc1MFKgE/SIyhnLXzmWMkrsKL4+NBWK/aTESoJuxC1V5R88g8OmBMCOCC6ZTTnMRfpPUZNcVclUNbai3YmdjigIXzEOQ2CchQWosdiiibgwMgG9pP/N7cMBujAVtEXZNjWci85TRec8gmcg0suo6A7xQPLm9NLKXSr2dZAoGAC8S1KdF5H0z8WOzMB1SMBqJrdcasJv2wZkKoimXEDS3zt9S5T38B1SvsW+V/QSdl9o9BWU4MvitlL3aCiKsaDIx1wNgZXDx7+kVXSieHwqEzKTPV/qhFuCDMFMrQ0aTi6Q5L5y+P4OP1lUpc6diH2L0++bTUoUBgTthnih6WsTc=";

    /** 商户应用公钥证书路径（证书模式下设置，公钥模式下无需设置） */
    @Getter
    private static String certPath = "D:\\Company\\aliPay\\appCertPublicKey_2021004125622328.crt";

    /** 支付宝公钥证书路径（证书模式下设置，公钥模式下无需设置） */
    @Getter
    private static String alipayPublicCertPath = "D:\\Company\\aliPay\\alipayCertPublicKey_RSA2.crt";

    /** 支付宝根证书路径（证书模式下设置，公钥模式下无需设置） */
    @Getter
    private static String rootCertPath = "D:\\Company\\aliPay\\alipayRootCert.crt";

    /** 网关地址 线上：https://openapi.alipay.com/gateway.do 沙箱：https://openapi.alipaydev.com/gateway.do */
    @Getter
    // private static String serverUrl = "https://openapi.alipaydev.com/gateway.do";
    private static String serverUrl = "https://openapi.alipay.com/gateway.do";

    @Getter
    private static Boolean isPro = false;

    public void setAppId(String appId) {
        AliPayConf.appId = appId;
    }

//    public void setPrivateKey(String privateKey) {
//        AliPayConf.privateKey = privateKey;
//    }

    public void setCertPath(String certPath) {
        AliPayConf.certPath = certPath;
    }

    public void setAlipayPublicCertPath(String alipayPublicCertPath) {
        AliPayConf.alipayPublicCertPath = alipayPublicCertPath;
    }

    public void setRootCertPath(String rootCertPath) {
        AliPayConf.rootCertPath = rootCertPath;
    }

    public void setServerUrl(String serverUrl) {
        AliPayConf.serverUrl = serverUrl;
    }

    public void setIsPro(boolean isTest) {
        AliPayConf.isPro = isTest;
    }

    public void setPrivateKeyPath(String privateKeyPath) {
        AliPayConf.privateKeyPath = privateKeyPath;
    }

    public void setIsPro(Boolean isPro) {
        AliPayConf.isPro = isPro;
    }

    public static String getPrivateKey() {
        if (privateKey != null && !privateKey.isEmpty()) {
            return privateKey;
        }
        try {
            FileInputStream fileInputStream = new FileInputStream(privateKeyPath);
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();

            int nRead;
            byte[] data = new byte[8192];
            while ((nRead = fileInputStream.read(data, 0, data.length)) != -1) {
                buffer.write(data, 0, nRead);
            }
            byte[] byteArray = buffer.toByteArray();
            return privateKey = new String(byteArray, StandardCharsets.UTF_8);
        } catch (FileNotFoundException e) {
            throw new GlobalException("支付宝私钥证书文件不存在");
        } catch (IOException e) {
            throw new GlobalException("支付宝私钥证读取失败");
        }
    }

}
